package net.tinyportal.portal;

import java.io.IOException;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.UnavailableException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tinyportal.Constant;
import net.tinyportal.javax.portlet.TpPortletContext;
import net.tinyportal.javax.portlet.TpPortletURL;
import net.tinyportal.javax.portlet.TpRenderRequest;
import net.tinyportal.javax.portlet.TpRenderResponse;
import net.tinyportal.service.PortletService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class PortletRender {

	/**
	 * Logger
	 */
	private static final Log log = LogFactory.getLog(PortletRender.class);

	@Autowired
	private PortletService portletService;

	@Autowired
	private PortalAction portalAction;

	@Autowired
	private TpPortletContext tpPortletContext;
	
	@Autowired
	private TpRenderRequest TPrequest;
	
	@Autowired
	private TpRenderResponse tpRenderResponse;
	
	@Autowired
	private User user;

	/**
	 * Effectue le rendu du portlet dont le nom est passé en paramètre
	 * @param request
	 * @param response
	 * @param portletName
	 * @return Retourne le portletHolder du portlet une fois le rendu effectué
	 */
	public PortletHolder render(HttpServletRequest request, HttpServletResponse response, String portletName) {
		/*
		 * On récupère le bean associé au portlet
		 */
		PortletHolder portletHolder = user.getHolder(portletName);
		GenericPortlet portlet = portletHolder.getPortlet();

		/*
		 * DOGET
		 */
		String portletId = TpPortletURL.getPortletId(portletHolder);

		/*
		 * On positionne le bean portlet dans la requête pour pouvoir y accéder depuis les taglibs
		 */
		TPrequest.setAttribute(Constant.session_portlet_bean,portletHolder);

		portletHolder.setRenderRequest(TPrequest);
		portletHolder.setRenderResponse(tpRenderResponse);

		try {
			/*
			 * On effectue les taches d'ACTION
			 */
			StringBuffer sb = new StringBuffer(portletId).append("_").append("action");
			String action = request.getParameter(sb.toString());
			if ("action".equalsIgnoreCase(action)) {
				portalAction.doAction(portletHolder, portlet);
			} else if ("render".equalsIgnoreCase(action)) {
				portalAction.doRender(portletHolder, portlet);
			} else {
				portalAction.setWindowState(request, portletHolder);
				portalAction.setPortletMode(request, portletHolder);
			}
			/*
			 * de RENDU
			 */
			portalAction.doRender(portletHolder, portlet);

			/*
			 * 
			 */
			try {
				portalAction.doRefresh(portletHolder, portlet);
			} catch (PortletException e) {
				log.warn("Error while doing portlet "+portletName,e);
				portletHolder.setTitle("Erreur sur le portlet");
				portletHolder.setContent("/!\\ Erreur !!" + e.getMessage());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (UnavailableException ue) {
			log.warn("Error while doing portlet "+portletName,ue);
			portlet.destroy();
//			portal.addDisabledPortlet(portletName);

		} catch (PortletException e) {
			log.warn("Error while doing portlet "+portletName,e);
			portletHolder.setTitle("Erreur sur le portlet");
			portletHolder.setContent("/!\\ Erreur !!" + e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
			return portletHolder;	
	}


}
